package cn.ix.spring.security.handler;

import com.google.common.base.Charsets;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lzw
 * @description MyAccessDeniedHandler
 * @date 2022/3/2
 */
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setCharacterEncoding(Charsets.UTF_8.name());
        response.setContentType("application/json");
        response.getWriter().println("{'code':'403','message':'没有访问权限'}");
        response.getWriter().flush();
    }
}